package com.visitorspecials.tankbattle.server;

public class Vector2
{
	public float x, y;
	
	public static Vector2 zero = new Vector2(0, 0);

	public Vector2 (float x, float y)
	{
		this.x = x;
		this.y = y;
	}
	
	public static float magnitude(Vector2 v)
	{
		return (float)Math.sqrt(v.x * v.x + v.y * v.y);
	}
	
	public float getAngle()
	{
		return HelperFuncs.radiansToDegrees((float) Math.atan2(this.y, this.x));
	}
	
	public static Vector2 fromPolar(float angle, float magnitude)
	{
		float x = (float) Math.cos(HelperFuncs.degreesToRadians(angle));
		float y = (float) Math.sin(HelperFuncs.degreesToRadians(angle));

		return new Vector2(x * magnitude, y * magnitude);
	}
	
	public static Vector2 add(Vector2 a, Vector2 b)
	{
		return new Vector2(a.x + b.x, a.y + b.y);
	}
	
	public static Vector2 scale(Vector2 a, float scale)
	{
		return new Vector2(a.x * scale, a.y * scale);
	}
	
	public static Vector2 subtract(Vector2 minuend, Vector2 subtrahend)
	{
		return new Vector2(minuend.x - subtrahend.x, minuend.y - subtrahend.y);
	}
	
	public void Normalize()
	{
		float length = Vector2.magnitude(this);
		this.x /= length;
		this.y /= length;
	}
	
	public static float AngleFromVector(Vector2 vector)
    {
        return 90 + HelperFuncs.radiansToDegrees(
            (float)Math.atan2(vector.y, vector.x));
    }
}
